package com.sen.common.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import com.sen.common.common.exception.BizParamException;
import com.sen.common.common.response.ResultMsgEnum;

import java.util.Map;

/**
 * @author esjiang
 */
public abstract class CheckUtils {

    /**
     * 参数非空校验
     *
     * @param object
     * @param attributes
     */
    public static void checkNotNull(Object object, String... attributes) {

        if (ObjectUtil.isNull(object)) {
            throw new BizParamException(ResultMsgEnum.PARAM_IS_NULL);
        }
        if (attributes != null && attributes.length > 0) {
            for (String attribute : attributes) {
                Object fieldValue;
                if (object instanceof Map) {
                    fieldValue = ((Map) object).get(attribute);
                } else {
                    fieldValue = ReflectUtil.getFieldValue(object, attribute);
                }
                if (ObjectUtil.isEmpty(fieldValue)) {
                    throw new BizParamException(ResultMsgEnum.PARAM_IS_NULL.fillMessage(attribute));
                }

            }
        }
    }

}
